package com.chy.blog_server.modular.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.system.entity.SysUser;
import com.chy.blog_server.modular.web.entity.BoComment;
import com.chy.blog_server.modular.web.entity.BoTag;
import com.chy.blog_server.modular.web.service.IBoCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：评论/留言表 controller
 * 作者：chenhuiyan
 * 时间：2020/03/17 22:07
 */
@Controller
@RequestMapping(value = "/web/boComment")
public class BoCommentController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boComment/";

    @Autowired
    private IBoCommentService boCommentService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boComment";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boCommentService.list(params); //获取数据列表
            Integer count = boCommentService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 回复页面
     * @return
     */
    @RequestMapping(value = "/reply")
    public String reply(@RequestParam(name = "id") String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "reply";
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            BoComment boComment = boCommentService.getById(id);
            boComment.setIsRead(1); //修改状态为已读
            boCommentService.updateById(boComment); //更新状态
            res.put("boComment", boComment);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 留言回复
     * @param boComment
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(BoComment boComment, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            SysUser sysUser = UserUtil.getUserFromSession(request);
            boComment.preInsert();
            boComment.setByUserName(sysUser.getNickName());
            if("miao".equals(sysUser.getUserName())){
                boComment.setIsAdmin(1);
                boComment.setHeadPhoto("favatar.jpg");
            }else if("admin".equals(sysUser.getUserName())){
                boComment.setIsAdmin(2);
                boComment.setHeadPhoto("avatar.jpg");
            }
            boComment.setType(1);
            boComment.setIsRead(1);
            boComment.setTime(new Date());
            boCommentService.save(boComment);
            res.put("state", "200");
            res.put("message", "回复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "回复失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            BoComment boComment = boCommentService.getById(id);
            boComment.setDelFlag(1);
            boCommentService.updateById(boComment);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 评论/留言回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boCommentRecycle";
    }

    /**
     * 查询已删除的评论/留言列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boCommentService.recycleList(params); //获取数据列表
            Integer count = boCommentService.getBaseMapper().selectCount(new QueryWrapper<BoComment>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            int i = boCommentService.thoroughlyDelete(id);
            if(i != 0){
                res.put("state", "200");
                res.put("message", "删除成功");
            }else {
                res.put("state", "500");
                res.put("message", "删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复文章
     * @param id
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            BoComment boArticle = boCommentService.getById(id);
            boArticle.setDelFlag(0);
            boCommentService.updateById(boArticle);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
